import { transform, generateStep } from './transform';
export default class SceneIdGroupIdFormatter {
    uuid: string;
    defaultValue: {
        passage: number;
        group: number;
        scene: number;
    };
    constructor(uuid='scene_id_group_id', defaultValue=null) {
        this.uuid = uuid;
        this.defaultValue = {
            passage: 10,
            group: 1000,
            scene: 10,
        };
        if (defaultValue) {
            this.defaultValue = defaultValue;
        }
    }
    equal(source, target) {
        return source === target;
    }
    parser(val = ''){
        if(!val) {
            console.log(`DP ${this.uuid}出现异常数据 %s， 无法解析`, val);
            return this.defaultValue;
        }
        const generator = transform(val);
        generator.next();
        const step2 = generateStep(generator, 2);
        const step4 = generateStep(generator, 4);
        const passage = step2();
        const group = step4();
        const scene = step2();
        return {
            /**
             * 通道 最大数值256 最小值0
             */
            passage: Number.isNaN(passage) ? 0 : passage,
            /**
             * 组ID 最大数值65025 最小值0
             */
            group: Number.isNaN(group) ? 0 : group,
            /**
             * 场景ID 最大数值256 最小值0
             */
            scene: Number.isNaN(scene) ? 0 : scene,
        }
    }
    to16(value, length) {
        let result = Number(value).toString(16);
        if(result.length < length){
            result = result.padStart(length, '0');
        }
        return result;
    }
    formatter(data) {
        const {passage, group, scene} = data;
        const passageStr = this.to16(passage, 2);
        const groupStr = this.to16(group, 4);
        const sceneStr = this.to16(scene, 2);
        return `${passageStr}${groupStr}${sceneStr}`;
    }
}